package com.koron.order.maintain.service.impl;

import com.koron.common.core.util.CodeTools;
import com.koron.order.common.bean.po.OrderDispatch;
import com.koron.order.common.bean.po.OrderDispatchWorker;
import com.koron.order.common.service.OrderDispatchService;
import com.koron.order.common.service.OrderDispatchWorkerService;
import com.koron.order.maintain.bean.convertor.MaintainConvertor;
import com.koron.order.maintain.bean.dto.MaintainDispatchDto;
import com.koron.order.maintain.bean.entity.MaintainStatus;
import com.koron.order.maintain.bean.vo.MaintainDispatchVo;
import com.koron.order.maintain.service.MaintainDispatchService;
import com.koron.order.maintain.service.MaintainService;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.koron.ebs.mybatis.SessionFactory;
import org.koron.ebs.mybatis.TaskAnnotation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @ClassName MaintainDispatchServiceImpl
 * @description
 * @Author zhouj
 * @Date 2021/9/14 10:40
 */
@Service("maintainDispatchService")
public class MaintainDispatchServiceImpl implements MaintainDispatchService {

    @Autowired
    OrderDispatchService orderDispatchService;

    @Autowired
    OrderDispatchWorkerService orderDispatchWorkerService;

    @Autowired
    MaintainService maintainService;

    @Autowired
    MaintainConvertor maintainConvertor;

    @Override
    @TaskAnnotation("insertOrUpdate")
    public int insertOrUpdate(SessionFactory factory, MaintainDispatchDto dto) {

        OrderDispatch orderDispatch = maintainConvertor.dtoToBean(dto);

        String orderDispatchId = orderDispatch.getId();
        String orderId = orderDispatch.getOrderId();

        if (StringUtils.isEmpty(orderDispatchId)) {
            orderDispatch.setId(CodeTools.getCode32());
            orderDispatchService.insert(factory, orderDispatch);
        } else {
            orderDispatchService.update(factory, orderDispatch);
        }

        List<OrderDispatchWorker> orderDispatchWorker = dto.getOrderDispatchWorker();
        if (CollectionUtils.isEmpty(orderDispatchWorker)) {
            return 0;
        }
        orderDispatchWorker.forEach(p -> {
            p.setDispatchId(orderDispatch.getId());
            p.setOrderId(orderId);
        });
        //先删除再保存新增
        orderDispatchWorkerService.insertBatch(factory, orderDispatchWorker);

        if (dto.getIsCommit()) {
            //修改工单状态 待派工-->待执行
            maintainService.submitMaintain(factory, orderId, MaintainStatus.WAIT_EXECUTE.getStatus(), orderDispatch);

        }
        return 1;

    }

    @Override
    @TaskAnnotation("deleteById")
    public int deleteById(SessionFactory factory, String id) {
        orderDispatchService.deleteById(factory, id);
        orderDispatchWorkerService.deleteByDispatchId(factory, id);
        return 1;
    }

    @Override
    @TaskAnnotation("deleteByOrderId")
    public int deleteByOrderId(SessionFactory factory, String orderId) {
        orderDispatchService.deleteByOrderId(factory, orderId);
        orderDispatchWorkerService.deleteByOrderId(factory, orderId);
        return 1;
    }

    @Override
    @TaskAnnotation("queryByOrderId")
    public MaintainDispatchVo queryByOrderId(SessionFactory factory, String orderId) {
        //分别查出来,set进整个对象
        OrderDispatch orderDispatch = orderDispatchService.queryByOrderId(factory, orderId);
        MaintainDispatchVo maintainDispatchVo = maintainConvertor.beanToVo(orderDispatch);
        List<OrderDispatchWorker> orderDispatchWorkerList = orderDispatchWorkerService.queryByOrderId(factory, orderId);
        if (maintainDispatchVo!=null) {
            maintainDispatchVo.setOrderDispatchWorker(orderDispatchWorkerList);
        }
        return maintainDispatchVo;
    }

}
